package cc.fine.svc.main.service.impl.rel;

import cc.fine.svc.main.bean.po.RelUserVideoShare;
import cc.fine.svc.main.bean.po.TblUserCount;
import cc.fine.svc.main.mapper.RelUserVideoShareMapper;
import cc.fine.svc.main.service.VideoCountService;
import cc.fine.svc.main.service.rel.RelUserVideoShareService;
import cc.fine.svc.main.service.tbl.TblUserCountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wyihe
 * @since 2023-10-28
 */
@Service
@Transactional
public class RelUserVideoShareServiceImpl extends ServiceImpl<RelUserVideoShareMapper, RelUserVideoShare> implements RelUserVideoShareService {

    @Autowired
    private VideoCountService countService;

    @Autowired
    private TblUserCountService userCountService;

    @Override
    public void share(RelUserVideoShare share) {
        countService.increaseShare(share.getVideoId());
        save(share);

        // 更新 userCount 表
        TblUserCount userCount = userCountService.getById(share.getUserId());
        if (userCount == null) userCount = new TblUserCount();
        userCount.setShares(userCount.getShares() + 1);
        userCountService.saveOrUpdate(userCount);
    }
}
